﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;

namespace ButtonBack.ButtonBack
{
    [ToolboxItemAttribute(false)]
    public class ButtonBack : WebPart
    {
        protected override void CreateChildControls()
        {
            SPWeb mWeb = SPContext.Current.Web;
            String vName;
            string vText = SPContext.Current.RootFolderUrl;
            SPList list = SPContext.Current.List;
            String vWhere = "<Where><Eq><FieldRef Name=\"FileRef\"/><Value Type=\"Text\">" + vText + "</Value></Eq></Where>";
            SPListItemCollection mItems = RetItemQuery(list, vWhere);
            SPView dView = null;
            vName = vText;
            foreach(SPView mView in mItems.List.Views)
            {
                if (mView.DefaultView)
                {
                    dView = mView;
                    //string sU = dView.Url;
                }
            }
            if (mItems != null)
            {
                if (mItems.Count == 1)
                {
                    foreach (SPListItem expiredItem in mItems)
                    {
                        if (expiredItem["Поставщик"] != null)
                        {
                            Button vButton = new Button();
                            vButton.Text = "< Закрыть";
                            /*
                            Button vButton = new Button();
                            vButton.Text = "< Закрыть";
                            //--
                            string sButtonBack = "";
                            if (this.Page.Request.UrlReferrer != null)
                            {
                                sButtonBack = this.Page.Request.UrlReferrer.ToString();
                            }
                            else
                            {
                                vName = vText;
                                sButtonBack = vName.Substring(0, vName.IndexOf(expiredItem["Title"].ToString()));
                            }
                            //--
                            */
                            if (dView != null)
                                vButton.PostBackUrl = mWeb.Url + "/" +  dView.Url; //vName; sButtonBack
                            else
                                vButton.PostBackUrl = vName.Substring(0, vName.IndexOf(expiredItem["Title"].ToString()));
                            this.Controls.Add(vButton);
                        }
                    }
                }
            }
        }

        SPListItemCollection RetItemQuery(SPList mList, String mWhere)
        {
            SPListItemCollection mItem = null;
            //--
            SPQuery mQuery = new SPQuery();
            //-- 
            mQuery.Query = mWhere;
            mQuery.ViewAttributes = "Scope=\"RecursiveAll\" ModerationType=\"Moderator\"";
            //--
            mItem = mList.GetItems(mQuery);
            //--
            return mItem;
        }
    }
}
